﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Computer_Manager.BUS;
using Computer_Manager.DTO;

namespace Computer_Manager.GUI
{
    public partial class frmUser : DevExpress.XtraEditors.XtraForm
    {
        DTO_user dto_user = null;
        BUS_user bus_user = new BUS_user();
        public frmUser()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            frmAddUser frmAddU = new frmAddUser();
            //frmAddU.Parent = this;
            frmAddU.ShowDialog();
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            loadData();
            grvUser.Columns[0].Caption = "Mã người dùng";
            grvUser.Columns[1].Caption = "Tên tài khoản";
            grvUser.Columns[2].Caption = "Họ tên";
            grvUser.Columns[3].Caption = "Giới tính";
            grvUser.Columns[4].Caption = "Vai trò";
            grvUser.Columns[4].GroupIndex = 0;
            grvUser.Columns[4].Group();
        }
        public void loadData()
        {
            grcUser.DataSource = bus_user.GetAll();
        }

        private void btnEditUser_Click(object sender, EventArgs e)
        {
            dto_user = new DTO_user();
            frmAddUser frmAddU = new frmAddUser();
            frmAddU.id_user = int.Parse(grvUser.GetFocusedDataRow()[0].ToString());
            frmAddU.ShowDialog();
        }

        private void btnDelUser_Click(object sender, EventArgs e)
        {
           
            
            dto_user = new DTO_user();
            dto_user.id_user = int.Parse(grvUser.GetFocusedDataRow()[0].ToString());
            var data = bus_user.GetByID(dto_user);


            if (XtraMessageBox.Show("Bạn có chắc xóa tài khoản \"" + data.Rows[0]["username"].ToString() + "\"", "Thông tin", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (bus_user.user_Delete(dto_user))
                {
                    XtraMessageBox.Show("Bạn đã xóa thành công tài khoản " + data.Rows[0]["username"].ToString(), "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.loadData();
                }
            }
            
        }
    }
}